package com.keeson.android_cli.manager;


import com.keeson.android_cli.base.BaseEvent;
import com.keeson.android_cli.commom.EventCode;
import com.keeson.android_cli.protocol.TSICMDTPFactory;
import com.keeson.android_cli.protocol.TransmissionProtocol;
import com.keeson.android_cli.util.EventBusUtils;
import com.keeson.android_cli.util.HexDump;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;


public class ControlManager {
    private ScheduledExecutorService scheduledExecutor;
    private volatile static ControlManager client ;
    public ControlManager() {
        System.out.println(" created " + getClass().getSimpleName());
    }
    public static ControlManager getInstance() {
        if (client == null)
            synchronized (ControlManager.class) {
                if (client == null) {
                    client = new ControlManager();
                }
            }
        return client;
    }
    public void updateAddOrSubtract(Object data) {
        if(scheduledExecutor == null){
            scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        }
        scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                byte[] msg = (byte[]) data;
                EventBusUtils.sendEvent(new BaseEvent(EventCode.EVENT_BLE_CONN_SUCCESS, msg));
            }
        }, 0, 100, TimeUnit.MILLISECONDS);    //每间隔100ms发送Message
    }
    public void stopAddOrSubtract() {
        if (scheduledExecutor != null) {
            scheduledExecutor.shutdownNow();
            scheduledExecutor = null;
        }
    }



    public void Countdown(byte[] data,int param,int side) {
        Observable.interval(0, 200, TimeUnit.MILLISECONDS)
                .take(2)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        //LogUtils.e("aLong===",aLong+"");
                        if(aLong ==0){
                            EventBusUtils.sendEvent(new BaseEvent(EventCode.EVENT_BLE_SEND_DATA, data));
                        }else if(aLong ==1){
                            EventBusUtils.sendEvent(new BaseEvent(EventCode.EVENT_BLE_SEND_DATA, TSICMDTPFactory.getInstance().sendPackage(param, side, HexDump.reverseArray(TransmissionProtocol.KEY_GET_REALTIME_DATA))));
                        }

                    }
                }).doOnComplete(new Action() {
            @Override
            public void run() throws Exception {

                //EventBusUtils.sendEvent(new BaseEvent(EventCode.EVENT_BLE_SEND_DATA, TSICMDTPFactory.getInstance().sendPackage(param, side, HexDump.reverseArray(TransmissionProtocol.KEY_GET_REALTIME_DATA))));
            }
        }).subscribe();

    }








}
